<script setup>
import { ref } from 'vue'
import { RouterLink, RouterView } from 'vue-router'
import { useAuth } from './composables/useAuth'
import LoginModal from './components/LoginModal.vue'
import UserInfoModal from './components/UserInfoModal.vue'

const authState = useAuth()
const { isAuthenticated } = authState

const showLoginModal = ref(false)
const showUserInfoModal = ref(false)

function handleUserClick(e) {
  e.preventDefault()
  if (isAuthenticated.value) {
    showUserInfoModal.value = true
  } else {
    showLoginModal.value = true
  }
}

function handleLoginSuccess() {
  // 登录成功后可以自动打开用户信息弹窗
  showUserInfoModal.value = true
}

function handleLogout() {
  showUserInfoModal.value = false
}
</script>

<template>
  <div class="app-layout">
    <header class="app-header">
      <div class="brand">信息工程学展示与互动平台</div>
      <nav class="app-nav">
        <RouterLink to="/">首页</RouterLink>
        <RouterLink to="/introduction">学科简介</RouterLink>
        <RouterLink to="/shannon">奠基人</RouterLink>
        <RouterLink to="/elements">五大要素</RouterLink>
        <RouterLink to="/applications">应用领域</RouterLink>
        <RouterLink to="/future">未来展望</RouterLink>
        <RouterLink to="/forum">讨论区</RouterLink>
        <RouterLink to="/about">关于</RouterLink>
        <a href="#" @click.prevent="handleUserClick">用户</a>
      </nav>
    </header>
    <main class="app-main">
      <RouterView />
    </main>
    <LoginModal v-model:visible="showLoginModal" @success="handleLoginSuccess" />
    <UserInfoModal v-model:visible="showUserInfoModal" @logout="handleLogout" />
    <footer class="app-footer">
      © 信息工程学展示与互动平台
    </footer>
  </div>
</template>

<style scoped>
.app-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background-color: #1f2d3d;
  color: #ffffff;
}

.brand {
  font-size: 18px;
  font-weight: 600;
}

.app-nav {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.app-nav a {
  color: #ffffff;
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
}

.app-nav a.router-link-active {
  background-color: rgba(255, 255, 255, 0.18);
}

.app-nav a:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.app-nav a:last-child {
  cursor: pointer;
}

.app-main {
  flex: 1;
  background-color: #f5f7fa;
}

.app-footer {
  padding: 16px;
  text-align: center;
  font-size: 14px;
  color: #909399;
  background-color: #ffffff;
  border-top: 1px solid #ebeef5;
}
</style>
